﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Adams.Shared
{
    public static class Web
    {
        public static string GetUrl(string url)
        {
            string ret = string.Empty;
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.ContentType = "application/json; charset=UTF-8, */*";
            req.Accept = "application/json, text/javascript";

            try
            {
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                ret = GetStringFromHttpWebResponse(resp);
            }
            catch (WebException we)
            {
                WebResponse errResp = ((WebException)we).Response;
                ret = GetStringFromWebResponse(errResp);
                throw new Exception(ret);
            }
            return ret;
        }

        public static string GetUrlInXml(string url)
        {
            string ret = string.Empty;
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.ContentType = "application/xml";
            req.Accept = "application/xml";

            try
            {
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                ret = GetStringFromHttpWebResponse(resp);
            }
            catch (WebException we)
            {
                WebResponse errResp = ((WebException)we).Response;
                ret = GetStringFromWebResponse(errResp);
                throw new Exception(ret);
            }
            return ret;
        }

        public static string GetUrlWithJsonPost(string url, object data)
        {
            string ret = string.Empty;
            
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "POST";
            req.ContentType = "application/json; charset=UTF-8, */*";
            req.Accept = "application/json, text/javascript";

            string json = Adams.Shared.Serialize.SerializeJson(data);
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(json);
            req.ContentLength = byteData.Length;
            using (Stream postStream = req.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }

            try
            {
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                ret = GetStringFromHttpWebResponse(resp);
            }
            catch (WebException we)
            {
                WebResponse errResp = ((WebException)we).Response;
                ret = GetStringFromWebResponse(errResp);
                throw new Exception(ret);
            }

            return ret;
        }

        public static string GetUrlWithXmlPost(string url, object data)
        {
            string ret = string.Empty;

            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Method = "POST";
            req.ContentType = "application/json; charset=UTF-8, */*";
            req.Accept = "application/xml";

            string json = Adams.Shared.Serialize.SerializeJson(data);
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(json);
            req.ContentLength = byteData.Length;
            using (Stream postStream = req.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }

            try
            {
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                ret = GetStringFromHttpWebResponse(resp);
            }
            catch (WebException we)
            {
                WebResponse errResp = ((WebException)we).Response;
                ret = GetStringFromWebResponse(errResp);
                throw new Exception(ret);
            }

            return ret;
        }

        public static string GetStringFromHttpWebResponse(HttpWebResponse resp)
        {
            string ret = string.Empty;
            using (Stream stream = resp.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    ret = reader.ReadToEnd();
                    reader.Close();
                }
                stream.Close();
            }
            return ret;
        }

        public static string GetStringFromWebResponse(WebResponse resp)
        {
            string ret = string.Empty;
            using (Stream stream = resp.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    ret = reader.ReadToEnd();
                    reader.Close();
                }
                stream.Close();
            }
            return ret;
        }
    }
}
